﻿JSRequest.EnsureSetup();

var isIE7orBelow = false;
try { JSON } catch (err) { isIE7orBelow = true; };

$(document).ready(function () {
    /*
    //fix the breadcrumb to load page title
    if ($(".ms-ltviewselectormenuheader").length == 0
    && $(".row_pathnavigator span>span.die").length == 0
    && $(".row_pathnavigator").text().toLowerCase().indexOf("search results") == -1) {

    if ($.trim($(".links_navigator span.ms-WikiPageNameEditor-Display").text()) != $.trim($(".s4-breadcrumb span.s4-breadcrumbCurrentNode").text())
    && $.trim($(".links_navigator > span:last").clone().children().remove().end().text()) != $.trim($(".s4-breadcrumb span.s4-breadcrumbCurrentNode").text())) {
    $(".links_navigator").append($(".s4-breadcrumb span.s4-breadcrumbCurrentNode").clone().removeClass("s4-breadcrumbCurrentNode"));
    }
    }

    */

    //hide duplicated link for Publishing Page
    if ($.trim($(".links_navigator > span:first > span:last > a.breadcrumbCurrentNode").text()) == $.trim($(".links_navigator > span:last").text())) {
        $(".links_navigator > span:first > span:last").hide().prev().hide();
    }

    //remove selected class in case of wrong quicklaunch
    if ($(".menu-vertical a.selected").length > 1) {
        var $nav = $(".ms-quickLaunch .menu-vertical a.selected");
        $nav.removeClass("selected");
    }
    else {
        var arrCurPath = window.location.pathname.split("/");
        var $nav = $(".ms-quickLaunch .menu-vertical a.selected");
        if ($nav.length == 1) {
            var arrCurNavPath = $nav.attr("href").split("?")[0].split("/");

            var idx = 1;
            var isDifference = false;
            for (var i = arrCurNavPath.length - 1; i >= 0; i--) {
                if (arrCurNavPath[i] != arrCurPath[arrCurPath.length - idx]) {
                    isDifference = true;
                    break;
                }
                idx++;
            }
            if (isDifference == true) {
                $nav.removeClass("selected");
            }
        }
    }

    //Fix the scrollbar in Chrome browser
    var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
    if (isChrome) {
        //SharePoint function
        FixRibbonAndWorkspaceDimensions();

        //make sure ExecuteOrDelayUntilScriptLoaded works
        if (typeof (_spBodyOnLoadWrapper) != 'undefined') {
            _spBodyOnLoadWrapper();
        }
    }

    //fix picker's width for some browsers (ie: chrome)
    $("table[id$='_OuterTable']").each(function () {
        var $tbl = $(this);

        $tbl.width("100%");
        $tbl.find(">tbody>tr>td:first").css("width", "100%");
        $tbl.find("td:has(>a[id$='checkNames'])").attr("align", "right");
    });

    //remove !important for ms-vh2 class
    $(".ms-viewheadertr > .ms-vh2").css("border", "0");

    //format rating table in survey
    $("td.ms-formbodysurvey table[summary]>tbody>tr:odd").addClass("survey-oddrow");

    //fix width in dialog form
    if (JSRequest.QueryString["IsDlg"]) {
        $("#s4-bodyContainer").removeClass("width960");
        $(".ms-cui-ribbonTopBars").width("auto");
    }

    //force hide _invisibleIfEmpty
    $("td[name='_invisibleIfEmpty']:not(:has(img))").each(function () {
        if ($.trim($(this).text()) == "") {
            $(this).hide();
        }
    });

    //remove left menu last li border
    $(".left_menu_1 li:last").addClass("noBorder");

    //check if IE7 or below
    if (isIE7orBelow) {
        $("#s4-topheader2, #s4-mainarea, .footer-area").hide();
        $(".header_menu").html("<b>To access this portal, please use a supported browser, such as IE8 and above/Firefox.</b>");
        $("#aspnetForm").css("visibility", "visible");
    }

    //refresh document links when expand a group in Document Library (if any)
    $("a[onclick*='ExpCollGroup']").bind('click', function () {
        setTimeout("refreshDocumentLinks()", 2000);
    });
    //do the first change after page load
    refreshDocumentLinks();


    //move Search box to Body
    if ($(".srch-sb-results, .srch-sb-results7").length > 0) {
        $("#s4-leftpanel").hide();
        $(".s4-ca").css("margin-left", "0");
    }
    $(".srch-sb-results, .srch-sb-results7").prependTo($(".ms-bodyareacell"));
});

function refreshDocumentLinks() {
    //detect link in Document Library and change the behavior
    $("div[field='LinkFilename'] a[href*='.pdf']").removeAttr('onclick').attr("target", "_blank");
}